package com.golddaymicromall.activity.recharge;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.ZaiXianHuiKuanInfo;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import org.apache.http.conn.util.InetAddressUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class ZaiXianhuiKuanWebActivity extends BaseActivity {

    private String morderName;
    private int total_fee;
    private String Allprice;
    private String id;
    private String url;
    private String data = null;

    private WebView myibaowebview;

    ProgressBar progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zai_xianhui_kuan_web);
        morderName = getIntent().getStringExtra("morderName");
        Allprice = getIntent().getStringExtra("mAllPrice");
        total_fee = (int) (Float.valueOf(Allprice) * 100);
        id = SPUtils.get(getApplicationContext(), "Id", "").toString();
        attemptyibao();
        initView();
    }

    private void initView() {
        progress = (ProgressBar) findViewById(R.id.progress);
        myibaowebview = (WebView) findViewById(R.id.yibao_zhifu);
        myibaowebview.getSettings().setJavaScriptEnabled(true);
        myibaowebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

    }

    public void attemptyibao() {

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.onlineremittancet.add");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("orderid", morderName);
        form.put("identityid", id);
        form.put("amount", String.valueOf(total_fee));
        form.put("userip", GetIp().toString());
        form.put("transtime",String.valueOf(genTimeStamp()));
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            ZaiXianHuiKuanInfo zaiXianHuiKuanInfo = JSON.parseObject(result,ZaiXianHuiKuanInfo.class);
                            url=zaiXianHuiKuanInfo.getUrl();
                            myibaowebview.loadUrl(url);
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                },throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }

    private long genTimeStamp() {
        return System.currentTimeMillis() / 1000;
    }

    public String GetIp() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
                        .hasMoreElements(); ) {
                    InetAddress inetAddress = ipAddr.nextElement();
                    // ipv4地址
                    if (!inetAddress.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(inetAddress
                            .getHostAddress())) {

                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Exception ex) {
        }
        return "";

    }

}
